package com.jintoufs.service.hotel.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.hotel.HotelMapper;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.hotel.Hotel;
import com.jintoufs.domain.hotel.HotelRoom;
import com.jintoufs.domain.shop.Shop;
import com.jintoufs.domain.shop.ShopManager;
import com.jintoufs.logAnnotation.SystemServiceLog;
import com.jintoufs.service.hotel.HotelRoomService;
import com.jintoufs.service.hotel.HotelService;
import com.jintoufs.service.shop.ShopManagerService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.uuid.SnowflakeIdWorker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HotelServiceImpl implements HotelService {
    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    @Resource
    private HotelMapper hotelMapper;

    @Resource
    private HotelRoomService hotelRoomService;
    @Resource
    private ShopService shopService;

    @Resource
    private ShopManagerService shopManagerService;

    @Override
    public Map<String, Object> save(Hotel hotel, String id) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (ValidUtil.isEmpty(hotel.getId())) {
                hotel.setId(id);
                hotelMapper.insert(hotel);
                result.put("msg", "新增成功");
            } else {
                hotel.setStatus("0");
                hotelMapper.updateByPrimaryKeySelective(hotel);
                result.put("msg", "更新成功");
            }
            result.put("status", "OK");
            return result;
        } catch (Exception e) {
            result.put("msg", "操作失败");
            result.put("status", "ERROR");
            return result;
        }
    }

    @Override
    public Map<String, Object> save(Hotel hotel, String id, Customer customer) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (ValidUtil.isEmpty(hotel.getId())) {
                hotel.setId(id);
                hotel.setStatus("0");
                hotel.setCreateTime(new Date());
                hotel.setModifyTime(new Date());
                hotel.setCreateUserId(customer.getId());
                hotelMapper.insert(hotel);
                Shop shop = new Shop();
                shop.setName(hotel.getHotelName());
                shop.setValueId(hotel.getId());
                Shop now = shopService.save(shop,getUUID());
                ShopManager shopManager = new ShopManager();
                shopManager.setShopId(now.getId());
                shopManager.setCustomerId(customer.getId());
                shopManagerService.save(shopManager,getUUID());
                result.put("msg", "新增成功");
            } else {
                hotel.setModifyTime(new Date());
                hotelMapper.updateByPrimaryKeySelective(hotel);
                result.put("msg", "更新成功");
            }
            result.put("status", "OK");
            return result;
        } catch (Exception e) {
            result.put("msg", "操作失败");
            result.put("status", "ERROR");
            return result;
        }
    }

    @Override
    @SystemServiceLog(description = "删除酒店")
    public void delete(String id) {
        String[] ids = id.split(",");
        for (String idm:ids){
            Hotel hotel = hotelMapper.selectByPrimaryKey(idm);
//            hotelRoomService.deleteByHotelId(idm);
            hotel.setStatus("0");
//            hotelMapper.deleteByPrimaryKey(idm);
            hotelMapper.updateByPrimaryKeySelective(hotel);
        }
    }

    @Override
    @SystemServiceLog(description = "修改酒店信息")
    public void update(Hotel hotel) {
        hotel.setModifyTime(new Date());
        hotelMapper.updateByPrimaryKey(hotel);
    }

    @Override
    @SystemServiceLog(description = "新增酒店")
    public Hotel add(Hotel hotel) {
        Map<String, Object> params = new HashMap<>();
        params.put("hotelName", hotel.getHotelName());
        List<Map<String,Object>> list = hotelMapper.queryByParams(params);
        if (list == null || list.size() == 0) {
            hotel.setStatus("0");
            hotelMapper.insert(hotel);
            return hotel;
        } else {
            return null;
        }
    }

    @Override
    public List<Map<String,Object>> queryByParams(Map<String, Object> params) {
        return hotelMapper.queryByParams(params);
    }

    @Override
    public List<Map<String,Object>> selectByParams(Map<String, Object> params) {
        return hotelMapper.selectByParams(params);
    }

    @Override
    public List<Map<String,Object>> getByFilter(QueryFilters filters) {
        return hotelMapper.getByFilter(filters);
    }

    @Override
    public List<HotelRoom> getRoomList(String hotelId) {
        return hotelMapper.getRoomList(hotelId);
    }
    @Override
    public Hotel selectByPrimaryKey(String hotelId) {
        return hotelMapper.selectByPrimaryKey(hotelId);
    }

    @Override
    public List<Map<String, Object>> findByHotel(Map<String, Object> params) {
        return hotelMapper.findByHotel(params);
    }

    @Override
    public Map<String,Object> getById(String id) {
        return hotelMapper.queryByPrimaryKey(id);
    }

    public String getUUID(){
        return String.valueOf(snowflakeIdWorker.nextId());
    }


    @Override
    public void auth(String id,String status) {
        Hotel hotel = hotelMapper.selectByPrimaryKey(id);
        hotel.setStatus(status);
        hotelMapper.updateByPrimaryKey(hotel);
    }


    @Override
    public Integer getTotalCount() {
        return hotelMapper.getTotalCount();
    }

    @Override
    public Map<String, Object> getOnePriceOnHotel(String hotelId) {
        return hotelMapper.getOnePriceOnHotel(hotelId);
    }
}
